package com.mouse.hxsj.view;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mouse.damo.micro.*;
import com.mouse.damo.micro.Window;
import com.mouse.damo.ui.FileUtil;
import com.mouse.haoi.entity.Point;
import com.mouse.haoi.service.CustomService;
import com.mouse.hxsj.constants.ComPool;
import com.mouse.hxsj.constants.SystemParams;
import com.mouse.hxsj.dao.ShopMapper;
import com.mouse.hxsj.entity.common.Hwnd;
import com.mouse.hxsj.entity.enums.GoodsType;
import com.mouse.hxsj.entity.po.Shop;
import com.mouse.hxsj.entity.vo.Goods;
import com.mouse.hxsj.entity.vo.ShopVo;
import com.mouse.hxsj.service.ShopService;
import com.mouse.hxsj.util.BeanUtil;
import com.mouse.hxsj.util.JsonUtil;
import com.mouse.hxsj.util.StringUtil;
import com.mouse.hxsj.util.ViewGet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Component
public class ShopFrm extends JInternalFrame {
    private static CustomService customService = new CustomService();

    private JPanel contentPane;
    private JTextField textField;
    private JTable table;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    @Autowired
    private ShopMapper shopMapper;
    private JTable table_1;
    private JComboBox comboBox;
    JComboBox comboBox_2;
    JComboBox comboBox_3;
    JComboBox comboBox_1;
    private JTextField textField_6;
    JPanel panel_1;
    JComboBox<Hwnd> comboBox_4;
    private JTextField textField_7;
    private JTable table_2;
    JComboBox comboBox_5;
    JComboBox comboBox_6;
    JButton button_4;
    ShopService shopService = new ShopService();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ShopFrm frame = new ShopFrm();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ShopFrm() {
        setTitle("自动购物");
        setClosable(true);
        setIconifiable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(20, 20, 900, 520);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(null, "\u529F\u80FD", TitledBorder.LEADING, TitledBorder.TOP, null, null));

        JScrollPane scrollPane = new JScrollPane();

        panel_1 = new JPanel();
        panel_1.setBorder(new TitledBorder(null, "\u7F16\u8F91", TitledBorder.LEADING, TitledBorder.TOP, null, null));

        JScrollPane scrollPane_1 = new JScrollPane();
        scrollPane_1.setToolTipText("商品");

        JPanel panel_2 = new JPanel();
        panel_2.setBorder(new TitledBorder(null, "\u65E5\u5FD7", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
                                        .addComponent(panel, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                        .addComponent(panel_1, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 606, Short.MAX_VALUE)
                                        .addComponent(scrollPane, Alignment.LEADING))
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addComponent(scrollPane_1, GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                                        .addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(panel, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 213, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGap(10)
                                                .addComponent(scrollPane_1, 0, 0, Short.MAX_VALUE)))
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(panel_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGap(17)
                                                .addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))))
        );

        table_1 = new JTable();
        table_1.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "\u540D\u79F0", "\u54C1\u8D28", "\u4EF7\u683C"
                }
        ));
        table_1.getColumnModel().getColumn(0).setPreferredWidth(150);
        table_1.getColumnModel().getColumn(2).setPreferredWidth(150);
        scrollPane_1.setViewportView(table_1);

        JTextArea textArea = new JTextArea();
        GroupLayout gl_panel_2 = new GroupLayout(panel_2);
        gl_panel_2.setHorizontalGroup(
                gl_panel_2.createParallelGroup(Alignment.LEADING)
                        .addComponent(textArea, GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
        );
        gl_panel_2.setVerticalGroup(
                gl_panel_2.createParallelGroup(Alignment.LEADING)
                        .addComponent(textArea, GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
        );
        panel_2.setLayout(gl_panel_2);

        JLabel label_2 = new JLabel("类型1:");

        comboBox_2 = new JComboBox();
        Object[] array2 = Stream.of(GoodsType.values()).map(goodsType -> goodsType.getName()).toArray();
        comboBox_2.setModel(new DefaultComboBoxModel(array2));
        comboBox_2.addItemListener(e -> {
            List<String> list = GoodsType.getType2(e.getItem().toString());
            list.add(0, "全部");
            comboBox_3.setModel(new DefaultComboBoxModel(list.toArray()));
        });
        JLabel label_3 = new JLabel("类型2:");

        JLabel label_4 = new JLabel("匹配:");
        comboBox_3 = new JComboBox();

        JLabel label_5 = new JLabel("名称:");

        textField_1 = new JTextField();
        textField_1.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("等级1:");

        textField_2 = new JTextField();
        textField_2.setText("");
        textField_2.setColumns(10);

        JLabel label_6 = new JLabel("等级2:");

        textField_3 = new JTextField();
        textField_3.setText("");
        textField_3.setColumns(10);

        JLabel label_7 = new JLabel("价格:");

        textField_4 = new JTextField();
        textField_4.setText("");
        textField_4.setColumns(10);

        JLabel lblId = new JLabel("id:");

        textField_6 = new JTextField();
        textField_6.setEditable(false);
        textField_6.setColumns(10);

        JPanel panel_3 = new JPanel();

        JLabel lblNewLabel_3 = new JLabel("品质:");

        comboBox_5 = new JComboBox();
        comboBox_5.setModel(new DefaultComboBoxModel(new String[]{"全部", "紫色", "蓝色", "绿色", "白色"}));

        comboBox_6 = new JComboBox();
        comboBox_6.setModel(new DefaultComboBoxModel(new String[]{"模糊", "精确"}));
        GroupLayout gl_panel_1 = new GroupLayout(panel_1);
        gl_panel_1.setHorizontalGroup(
                gl_panel_1.createParallelGroup(Alignment.TRAILING)
                        .addGroup(gl_panel_1.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                                .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addComponent(comboBox_2, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(lblNewLabel_1)
                                                                        .addComponent(lblId))
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(textField_6, GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                                                                        .addComponent(textField_2, GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))))
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
                                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(label_6, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE))
                                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(comboBox_3, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
                                                                        .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)))
                                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                                .addComponent(lblNewLabel_3)
                                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                                .addComponent(comboBox_5, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addComponent(label_5)
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addComponent(textField_1, GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)))
                                .addGap(18)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addComponent(label_4)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(comboBox_6, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addComponent(label_7)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(textField_4, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)))
                                .addGap(28)
                                .addComponent(panel_3, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
                                .addGap(30))
        );
        gl_panel_1.setVerticalGroup(
                gl_panel_1.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel_1.createSequentialGroup()
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel_1.createSequentialGroup()
                                                .addGap(3)
                                                .addComponent(label_2))
                                        .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                                .addComponent(comboBox_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(label_3)
                                                .addComponent(comboBox_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(label_4)
                                                .addComponent(comboBox_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                .addGap(18)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_6)
                                        .addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label_7)
                                        .addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(7)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(textField_6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblId)
                                        .addComponent(lblNewLabel_3)
                                        .addComponent(comboBox_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(label_5)
                                        .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(28))
                        .addComponent(panel_3, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)
        );

        textField_7 = new JTextField();
        textField_7.setText("");
        textField_7.setColumns(10);
        textField_7.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyChar() == KeyEvent.VK_ENTER) {   //按回车键执行相应操作;
                    initTable2(textField_7.getText());
                }
            }
        });
        JScrollPane scrollPane_2 = new JScrollPane();
        GroupLayout gl_panel_3 = new GroupLayout(panel_3);
        gl_panel_3.setHorizontalGroup(
                gl_panel_3.createParallelGroup(Alignment.LEADING)
                        .addComponent(scrollPane_2, GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                        .addGroup(gl_panel_3.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(87, Short.MAX_VALUE))
        );
        gl_panel_3.setVerticalGroup(
                gl_panel_3.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel_3.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(textField_7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(scrollPane_2, GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
        );

        table_2 = new JTable();
        table_2.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "\u5546\u54C1"
                }
        ));
        scrollPane_2.setViewportView(table_2);
        panel_3.setLayout(gl_panel_3);
        panel_1.setLayout(gl_panel_1);

        table = new JTable();
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent met) {
                tableSelected(met);
            }
        });
        table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "id", "\u7C7B\u578B1", "\u7C7B\u578B2", "\u5339\u914D", "\u540D\u79F0", "\u7B49\u7EA71", "\u7B49\u7EA72", "\u4EF7\u683C", "品质"
                }
        ));
        table.getColumnModel().getColumn(4).setPreferredWidth(300);
        table.getColumnModel().getColumn(7).setPreferredWidth(150);

        scrollPane.setViewportView(table);

        JLabel lblNewLabel = new JLabel("类型1:");

        comboBox = new JComboBox();
        List<String> collect = Stream.of(GoodsType.values()).map(goodsType -> goodsType.getName()).collect(Collectors.toList());
        collect.add(0, "全部");
        comboBox.setModel(new DefaultComboBoxModel(collect.toArray()));
        comboBox.addItemListener(e -> {
            List<String> list = GoodsType.getType2(e.getItem().toString());
            list.add(0, "全部");
            comboBox_1.setModel(new DefaultComboBoxModel(list.toArray()));
        });
        JLabel label = new JLabel("类型2:");

        comboBox_1 = new JComboBox();

        JLabel label_1 = new JLabel("名称:");

        textField = new JTextField();
        textField.setColumns(10);

        JButton button = new JButton("搜索");
        button.addActionListener(e -> {
            initTable();
        });

        JButton button_1 = new JButton("新增");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addShop();
            }
        });

        JButton button_2 = new JButton("保存");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                saveShop();
            }
        });

        JButton button_3 = new JButton("删除");
        button_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                deleteShop();
            }
        });

        button_4 = new JButton("启动");
        button_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                startLoad();
            }
        });

        JLabel lblNewLabel_2 = new JLabel("句柄:");

        comboBox_4 = new JComboBox();
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addComponent(lblNewLabel)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
                                                .addGap(27)
                                                .addComponent(label, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                                                .addGap(4)
                                                .addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(label_1)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(textField, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
                                                .addGap(10)
                                                .addComponent(button_2, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
                                                .addGap(18)
                                                .addComponent(button_3, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
                                                .addGap(57)
                                                .addComponent(lblNewLabel_2)
                                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                                .addComponent(comboBox_4, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE)))
                                .addGap(18)
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
                                        .addComponent(button, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(button_4, GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE))
                                .addContainerGap(27, Short.MAX_VALUE))
        );
        gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                                .addComponent(lblNewLabel)
                                                .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(3)
                                                .addComponent(label))
                                        .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                                .addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(label_1)
                                                .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(button)))
                                .addPreferredGap(ComponentPlacement.UNRELATED)
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                                .addComponent(button_1)
                                                .addComponent(button_2)
                                                .addComponent(button_3))
                                        .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                                .addComponent(button_4)
                                                .addComponent(comboBox_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(lblNewLabel_2)))
                                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panel.setLayout(gl_panel);
        contentPane.setLayout(gl_contentPane);

    }

    static Thread thread = null;

    private void startLoad() {
        int selectedIndex = comboBox_4.getSelectedIndex();
        if (selectedIndex == -1) return;
        if (button_4.getText().equals("关闭")) {
            button_4.setText("开启");
            thread.stop();
            return;
        }
        List<Com> comList = ComPool.getComList();
        Com com = comList.get(selectedIndex);
        List<Shop> shopList = shopMapper.selectList(null);
        if (shopList.size() == 0) return;
        List<ShopVo> shopVoList = BeanUtil.copyList(shopList, ShopVo.class);
        button_4.setText("关闭");
        shopList.stream().filter(shop -> "全部".equals(shop.getType2()) && shop.getType1().equals("装备")).forEach(shop -> {
            String type1 = shop.getType1();
            for (String type2 : GoodsType.getType2(type1).subList(0, 3)) {
                shop.setType2(type2);
                shopVoList.add(BeanUtil.copyObject(shop, ShopVo.class));
            }
        });
        Map<String, List<String>> typeMap = new LinkedHashMap<>();
        for (GoodsType type : GoodsType.values()) {
            for (String type2 : type.getDetail()) {
                boolean match = shopVoList.stream().anyMatch(shopVo -> type.getName().equals(shopVo.getType1()) && type2.equals(shopVo.getType2()));
                if (match) {
                    List<String> list = typeMap.get(type.getName());
                    list = list == null ? new ArrayList<>() : list;
                    list.add(type2);
                    typeMap.put(type.getName(), list);
                }
            }
        }
        Map<String, List<ShopVo>> dataMap = shopVoList.stream().collect(Collectors.groupingBy(shop -> shop.getType1() + "-" + shop.getType2()));
        thread = new Thread(() -> {
            while (true) {
                DefaultTableModel tableModel = (DefaultTableModel) table_1.getModel();
                tableModel.setRowCount(0);
                ShopService.closeType(com);
                for (Map.Entry<String, List<String>> entry1 : typeMap.entrySet()) {
                    int index = 0;
                    List<String> value = entry1.getValue();
                    for (String s : value) {
                        List<ShopVo> shopList1 = dataMap.get(entry1.getKey() + "-" + s);
                        long t1 = System.currentTimeMillis();
                        while (shop(com, shopList1, entry1.getKey(), s)) {
                            Sleep.sleep(2000);
                        }
                        long diff = System.currentTimeMillis() - t1;
                        if (diff < 2000) Sleep.sleep((int) diff);
                        ShopService.closeType(com);
                    }
                }
            }
        });
        thread.start();
    }

    private boolean shop(Com com, List<ShopVo> shopList, String type1, String type2) {
        com.setResourcePath("D:\\damo");
        com.clearDict(5);
        com.setDict(5, "商店.txt");
        com.setDict(6, "价格.txt");
        int hwnd = com.getHwnd();
        // 查询窗口宽高
        com.mouse.damo.micro.Window window = new Window(com);
        int[] windowRect = window.getClientSize(hwnd);
        System.out.println("窗口宽高" + JsonUtil.toJson(windowRect));
        int height = windowRect[2], width = windowRect[1];

        ShopService.findType(com, height, width, type1, type2);
        return shopDetail(com, shopList, type1, type2, height, width);
    }

    private boolean shopDetail(Com com, List<ShopVo> shopList, String type1, String type2, int height, int width) {
        Sleep.sleep(300);
        List<Goods> goodsList = ShopService.trunGoods(com, height, width);
        for (Goods goods : goodsList) {
            boolean result = ShopService.buyGoods(com, goods, height, width, shopList);
            DefaultTableModel tableModel = (DefaultTableModel) table_1.getModel();
            Vector vector = new Vector();
            vector.add(goods.getName());
            vector.add(goods.getColor());
            vector.add(StringUtil.formatInt(goods.getPrice()));
            tableModel.addRow(vector);
            if (result) return true;
        }
        //向下滚动
        FindPic findPic = new FindPic(com);
        Point point1 = customService.loopPoint(50, () -> new Point(
                findPic.findPic(0, 0, width, height, "login/摆摊查询.bmp", "203040", 0.9, 0)));
        if (point1.getState() == -1) return false;
        int[] pic = findPic.findPic(point1.getX(), 0, width, height, "login/商店滚动.bmp", "203040", 0.9, 0);
        //不存在向下滚动
        if (pic[0] == -1) return false;
        //向下滚动
        Mouse mouse = new Mouse(com);
        mouse.mouseClick(pic[1], pic[2], true);
        Sleep.sleep(20);
        for (int i = 0; i < 7; i++) {
            mouse.wheelDown();
            Sleep.sleep(20);
        }
        Sleep.sleep(30);
        //是否滚动到了商店底部
        int[] pic2 = findPic.findPic(point1.getX(), 0, width, height, "login/商店底部.bmp", "203040", 0.9, 0);
        //不存在底部
        if (pic2[0] == -1) return shopDetail(com, shopList, type1, type2, height, width);
        //点击下一页
        mouse.mouseClick(point1.getX() + 176, point1.getY() + 333, true);
        FindStr findStr = new FindStr(com);
        //判断是不是最后一页
        boolean flag = customService.loop(1, () -> findStr.findStrE(0, 0, width, height, "后一页",
                "ff4200-000000", 0.9, 6)[0] != -1);
        //不是最后一页
        if (!flag) return shopDetail(com, shopList, type1, type2, height, width);
        return false;
    }


    private void initHwnd() {
        log.info("检测绑定句柄");
        List<Com> comList = ComPool.getComList();
        List<String> accountList = ComPool.getAccountList();
        comboBox_4.removeAllItems();
        for (int i = 0; i < comList.size(); i++) {
            Hwnd hwnd = new Hwnd();
            hwnd.setHwnd(comList.get(i).getHwnd());
            hwnd.setAccount(accountList.get(i));
            comboBox_4.addItem(hwnd);
        }
    }

    private void saveShop() {
        String type1 = ViewGet.comboBox(comboBox_2, null, String.class);
        String type2 = ViewGet.comboBox(comboBox_3, null, String.class);
        String matchType = ViewGet.comboBox(comboBox_6, null, String.class);
        String name = ViewGet.textField(textField_1);
        Integer grade1 = ViewGet.textField(textField_2, Integer.class);
        Integer grade2 = ViewGet.textField(textField_3, Integer.class);
        Integer price = ViewGet.textField(textField_4, Integer.class);
        String color = ViewGet.comboBox(comboBox_5, null, String.class);
        if (StringUtil.isBlank(type2)) {
            JOptionPane.showMessageDialog(null, "类型2不能为空");
            return;
        }
        if (StringUtil.isBlank(matchType)) {
            JOptionPane.showMessageDialog(null, "匹配不能为空");
            return;
        }
        if (StringUtil.isBlank(name)) {
            JOptionPane.showMessageDialog(null, "名称不能为空");
            return;
        }
        if (grade1 == null || grade1 < -1) {
            JOptionPane.showMessageDialog(null, "等级1不能为空");
            return;
        }
        if (grade2 == null || grade2 < -1) {
            JOptionPane.showMessageDialog(null, "等级2不能为空");
            return;
        }
        if (price == null || price < -1) {
            JOptionPane.showMessageDialog(null, "价格不能为空");
            return;
        }

        Shop shop = new Shop();
        shop.setType1(type1);
        shop.setType2(type2);
        shop.setMatchType(matchType);
        shop.setName(name);
        shop.setGrade1(grade1);
        shop.setGrade2(grade2);
        shop.setPrice(price);
        shop.setColor(color);
        Integer id = ViewGet.textField(textField_6, Integer.class);
        if (id == null) {
            shopMapper.insert(shop);
        } else {
            shop.setId(id);
            shopMapper.updateById(shop);
        }
        initTable();
    }

    private void deleteShop() {
        Integer id = ViewGet.textField(textField_6, Integer.class);
        if (id == null) return;
        shopMapper.deleteById(id);
        initTable();
    }

    private void addShop() {
        panel_1.setBorder(new TitledBorder(null, "新增", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        comboBox_6.setSelectedIndex(0);
        textField_2.setText("0");
        textField_3.setText("0");
        textField_6.setText("");
        textField_1.setText("");
        textField_4.setText("");
        comboBox_2.setSelectedIndex(0);
        comboBox_2.setSelectedIndex(1);
        comboBox_2.setSelectedIndex(0);
        comboBox_5.setSelectedIndex(0);
    }

    private List<Shop> initTable() {
        if ("开启".equals(button_4.getText())) initHwnd();
        if (shopMapper == null) return new ArrayList<>();
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setRowCount(0);
        Object type1 = ViewGet.comboBox(comboBox, null, "");
        Object type2 = ViewGet.comboBox(comboBox_1, null, "");
        LambdaQueryWrapper<Shop> wrapper = new LambdaQueryWrapper<Shop>().like(Shop::getName, textField.getText());
        if (StringUtil.isNotBlank(type1.toString()) && !"全部".equals(type1.toString())) {
            wrapper.eq(Shop::getType1, type1);
        }
        if (StringUtil.isNotBlank(type2.toString()) && !"全部".equals(type2.toString())) {
            wrapper.eq(Shop::getType2, type2);
        }
        List<Shop> shopList = shopMapper.selectList(wrapper);
        for (Shop shop : shopList) {
            Vector vector = new Vector();
            vector.add(shop.getId());
            vector.add(shop.getType1());
            vector.add(shop.getType2());
            vector.add(shop.getMatchType());
            vector.add(shop.getName());
            vector.add(shop.getGrade1());
            vector.add(shop.getGrade2());
            vector.add(StringUtil.formatInt(shop.getPrice()));
            vector.add(shop.getColor());
            tableModel.addRow(vector);
        }
        return shopList;
    }

    private void tableSelected(MouseEvent met) {
        panel_1.setBorder(new TitledBorder(null, "编辑", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        int row = table.getSelectedRow();
        Integer roleId = (Integer) table.getValueAt(row, 0);
        textField_6.setText(table.getValueAt(row, 0).toString());
        comboBox_2.setSelectedItem(table.getValueAt(row, 1));
        comboBox_3.setSelectedItem(table.getValueAt(row, 2));
        comboBox_6.setSelectedItem(table.getValueAt(row, 3).toString());
        textField_1.setText(table.getValueAt(row, 4).toString());
        textField_2.setText(table.getValueAt(row, 5).toString());
        textField_3.setText(table.getValueAt(row, 6).toString());
        textField_4.setText(table.getValueAt(row, 7).toString().replace(",", ""));
        comboBox_5.setSelectedItem(table.getValueAt(row, 8));

    }

    public void initFrm() {
        initTable();
        comboBox.setSelectedIndex(0);
        comboBox.setSelectedIndex(1);
        comboBox.setSelectedIndex(0);
        comboBox_2.setSelectedIndex(0);
        comboBox_2.setSelectedIndex(1);
        comboBox_2.setSelectedIndex(0);
        initHwnd();
        initTable2("");
    }

    private void initTable2(String key) {
        DefaultTableModel tableModel = (DefaultTableModel) table_2.getModel();
        tableModel.setRowCount(0);
        String txt = FileUtil.read(new File(SystemParams.sourcePath, "商店.txt").getAbsolutePath(), "GBK");
        for (String line : txt.split("\n")) {
            if (StringUtil.isBlank(line)) continue;
            String name = line.split("\\$")[1];
            if (StringUtil.isNotBlank(key) && !name.contains(key)) continue;
            Vector vector = new Vector();
            vector.add(name);
            tableModel.addRow(vector);
        }
    }
}
